package com.sunnybear.framework.tools;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * 软键盘显示隐藏监听
 *  2018/5/18.
 */
public class SoftKeyBoardListener {

    private View mRootView;//activity的根视图
    int mRootViewVisibleHeight;//纪录根视图的显示高度
    private OnSoftKeyBoardChangeListener mOnSoftKeyBoardChangeListener;

    private SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        mRootView = activity.getWindow().getDecorView();
        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        mRootView.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        //获取当前根视图在屏幕上显示的大小
                        Rect r = new Rect();
                        mRootView.getWindowVisibleDisplayFrame(r);

                        int visibleHeight = r.height();
                        System.out.println("" + visibleHeight);
                        if (mRootViewVisibleHeight == 0) {
                            mRootViewVisibleHeight = visibleHeight;
                            return;
                        }

                        //根视图显示高度没有变化，可以看作软键盘显示／隐藏状态没有改变
                        if (mRootViewVisibleHeight == visibleHeight) return;

                        //根视图显示高度变小超过200，可以看作软键盘显示了
                        if (mRootViewVisibleHeight - visibleHeight > 200) {
                            if (mOnSoftKeyBoardChangeListener != null)
                                mOnSoftKeyBoardChangeListener.keyBoardShow(mRootViewVisibleHeight - visibleHeight);
                            mRootViewVisibleHeight = visibleHeight;
                            return;
                        }

                        //根视图显示高度变大超过200，可以看作软键盘隐藏了
                        if (visibleHeight - mRootViewVisibleHeight > 200) {
                            if (mOnSoftKeyBoardChangeListener != null)
                                mOnSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - mRootViewVisibleHeight);
                            mRootViewVisibleHeight = visibleHeight;
                            return;
                        }
                    }
                });
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        mOnSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public static void setOnSoftKeyBoardChangeListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
    }

    public interface OnSoftKeyBoardChangeListener {

        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }
}
